package www.rlicker.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.Agent;
import www.rlicker.model.AgentUser;
import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IAgentService;
import www.rlicker.service.IAgentUserService;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.service.impl.SendNotificationService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.StringUtil;

import com.alibaba.fastjson.JSONArray;

@Controller  
@RequestMapping("/agent")
public class AgentController extends BaseController{
	
	@Autowired
	private IAgentService service;
	
	@Autowired
	private IAgentUserService agentUserService;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@RequestMapping("/showlist")
	public String showlist(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
        map.put("id", request.getParameter("id"));
		return "agent/agentList";
	}
	
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
		 Criteria<Agent> param = new Criteria<Agent>();
		 if (!StringUtil.isNullOrBlank(request.getParameter("nickname"))) {
			 param.addParam("nickname", request.getParameter("nickname"));
		 }
         if (!StringUtil.isNullOrBlank(request.getParameter("groupName"))) {
        	 param.addParam("group_name", request.getParameter("groupName"));
         }
         if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
      	   param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
         }
         if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
      	   param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
         }
	     List<Map<String,Object>> list = service.queryPage(param);
	     PageResult pageResult = new PageResult();
	     pageResult.setTotal(service.queryPageCount(param));
		 pageResult.setRows(list);
		 return pageResult;	
	}

	
	@RequestMapping("/showauditor")
	public String showAuditing(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
		return "agent/agentAuditingList";
	}
	
	@RequestMapping("/querypagelist")
	@ResponseBody
	public PageResult queryPageList(HttpServletRequest request){
		 Criteria<Agent> param = new Criteria<Agent>();
		 if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
			 param.addParam("status", request.getParameter("status"));
		 }
         if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
      	   param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
         }
         if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
      	   param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
         }
	     List<Map<String,Object>> list = service.queryPageList(param);
	     PageResult pageResult = new PageResult();
	     pageResult.setTotal(service.queryPageListCount(param));
		 pageResult.setRows(list);
		 return pageResult;	
	}
	
	@ResponseBody
	@RequestMapping(value="/update" , method = { RequestMethod.POST })
	public JsonResult update(HttpServletRequest request){
		JsonResult jr = JsonResult.newInstance(); 
	    String content = null;
		Criteria<Agent> param = new Criteria<Agent>();
		Agent agent = new Agent();
		if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
			agent.setStatus(request.getParameter("status"));
			if (request.getParameter("status").equals(STR_1)) {
			    content ="审核不通过"+request.getParameter("groupName")+"团队";
			}else {
				content ="审核通过"+request.getParameter("groupName")+"团队";
				Agent entity = service.queryEntity(Long.valueOf(request.getParameter("id")));
				if (entity != null) {
					AgentUser user = new AgentUser();
					user.setGroupId(entity.getId());
					user.setCreatedAt(new Date());
					user.setUpdatedAt(new Date());
					user.setVersion(RETURN_STRING);
					user.setUserId(entity.getUserId());
					agentUserService.insert(user);
				}
				SendNotificationService sendNoti = new SendNotificationService();
				sendNoti.sendToUser(entity.getUserId(), "10", "你的团队已审核成功!");
			}
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("selectd"))) {
			agent.setSelectd(request.getParameter("selectd"));
			if (request.getParameter("selectd").equals(STR_1)) {
			    content ="封团了"+request.getParameter("groupName")+"团队";
			}else {
				content ="解封了"+request.getParameter("groupName")+"团队";
			}
		}
		param.setRecord(agent);
		param.addParam("id", request.getParameter("id"));
	    String ret = service.update(param);
	    insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
}